# Created by 黄景涛
# DATE: 2025/5/21
import logging

import yaml
from logging import getLogger
from jsonpath import jsonpath

logging.basicConfig(
    format="%(asctime)s - %(levelname)s - %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S",
    level=logging.DEBUG
)

logger = getLogger()

with open('data.yaml', mode='r', encoding='utf8') as f:
    json_data = yaml.safe_load(f)

# eg1
pattern = '$.students[0].name'
logger.info(f'eg1结果：{jsonpath(json_data, pattern)}')

# eg2
pattern = '$..name'
logger.info(f'eg2结果：{jsonpath(json_data, pattern)}')

# eg3
pattern = '$.students[*].school'
logger.info(f'eg3结果：{jsonpath(json_data, pattern)}')

# eg4
pattern = '$.students[?(@.age < 18)]'
logger.info(f'eg4结果：{jsonpath(json_data, pattern)}')

# eg5
pattern = '$.store.*'
logger.info(f'eg5结果：{jsonpath(json_data, pattern)}')

# eg6
pattern = '$.students[0,2].name'
logger.info(f'eg6结果：{jsonpath(json_data, pattern)}')

# eg7
pattern = '$.students[1:3:2].name'
logger.info(f'eg7结果：{jsonpath(json_data, pattern)}')